<template>
  <div class="app-container">
    <!-- 添加或修改站内消息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body v-drag-fullscreen>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
            <el-form-item label="消息标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入消息标题" :disabled="disabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
            <el-form-item label="接收类型" prop="receiveType">
              <el-select v-model="form.receiveType" placeholder="请选择接收类型" :disabled="disabled">
                <el-option
                  v-for="item in dict.receive_type"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24" :xs="24" v-if="form.receiveType=='USER'">
            <el-form-item label="指定用户" prop="userIds">
              <el-select
                v-model="userIds"
                multiple
                placeholder="请选择指定用户">
                <el-option
                  v-for="item in userOptions"
                  :key="item.userName"
                  :label="item.nickName"
                  :value="item.userName">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>


          <el-col :span="24" :xs="24">
            <el-form-item label="消息内容">
              <el-input
                type="textarea"
                placeholder="请输入消息内容"
                v-model="form.content"
                show-word-limit
                :disabled="disabled"
                :autosize="{ minRows: 4, maxRows: 6 }"
              />

            </el-form-item>
          </el-col>
          <!--<el-col :span="8" :xs="24">-->
            <!--<el-form-item label="截止有效期" prop="efficientTime">-->
              <!--<el-date-picker clearable size="small" :disabled="disabled"-->
                              <!--v-model="form.efficientTime"-->
                              <!--type="date"-->
                              <!--value-format="yyyy-MM-dd"-->
                              <!--placeholder="选择截止有效期">-->
              <!--</el-date-picker>-->
            <!--</el-form-item>-->
          <!--</el-col>-->

        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {getNoticeSiteMessage, addNoticeSiteMessage, updateNoticeSiteMessage} from "@/api/notice/noticeSiteMessage";
  import { listUser } from "@/api/system/user";
  export default {
    name: "NoticeSiteMessageFrom",
    dicts: ['message_type', 'receive_type', 'send_state',],
    data() {
      return {
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //是否禁用
        disabled: false,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          title: [
            { required: true, message: "消息标题不能为空", trigger: "blur" }
          ],
          receiveType: [
            { required: true, message: "消息类型不能为空", trigger: "change" }
          ]
        },
        //用户字典
        userOptions:[],
        userIds:[]
      };
    },
    created() {
      listUser({}).then(res=>{
        this.userOptions = res.rows
      })
    },
    methods: {
      show(option) {
        this.reset();
        this.disabled = option.type == 'VIEW'
        if (option.type == 'ADD') {
          this.open = true;
          this.title = "新增站内消息";
        } else if (option.type == 'UPDATE' || option.type == 'VIEW') {
          getNoticeSiteMessage(option.id).then(response => {
            this.form = response.data;
            this.userIds = this.$utils.formatJson(this.form.userIds)
            if (option.type == 'VIEW') {
              this.title = "查看站内消息";
            } else {
              this.title = "修改站内消息";
            }
            this.open = true;
          });
        }
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
        this.$emit("cancel")
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          title: null,
          content: null,
          senderId: null,
          senderName: null,
          senderTime: null,
          messageType: null,
          receiveType: 'ALL',
          sendState: null,
          efficientTime: null,
          userIds:[]
        };
        this.resetForm("form");
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.userIds = this.$utils.toJson(this.userIds)
            if (this.form.id != null) {
              updateNoticeSiteMessage(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.cancel()
              });
            } else {
              addNoticeSiteMessage(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.cancel()
              });
            }
          }
        });
      }
    }
  };
</script>
